//
//  GMAFNetWork.m
//  News
//
//  Created by 雷国敏 on 16/8/18.
//  Copyright © 2016年 lieop. All rights reserved.
//

#import "GMAFNetWork.h"

@implementation GMAFNetWork
#pragma mark GET
/**
 *  普通get方法请求网络数据
 *
 *  @param url     请求网址路径
 *  @param params  请求参数
 *  @param success 成功回调
 *  @param fail    失败回调
 */
+(void)GET:(NSString *)url params:(NSDictionary *)params
   success:(GMResponseSuccess)success fail:(GMResponseFail)fail{
	
	AFHTTPSessionManager *manager = [GMAFNetWork managerWithBaseURL:nil sessionConfiguration:NO];
//	manager.requestSerializer.timeoutInterval = 5;
	
	[manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		LxDBAnyVar(task.currentRequest.URL.absoluteString);
		id dic = [GMAFNetWork responseConfiguration:responseObject];
		
		success(task,dic);
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		fail(task,error);
	}];
}



/**
 *  含有baseURL的get方法
 *
 *  @param url     请求网址路径
 *  @param baseUrl 请求网址根路径
 *  @param params  请求参数
 *  @param success 成功回调
 *  @param fail    失败回调
 */
+(void)GET:(NSString *)url baseURL:(NSString *)baseUrl params:(NSDictionary *)params
   success:(GMResponseSuccess)success fail:(GMResponseFail)fail{
	
	AFHTTPSessionManager *manager = [GMAFNetWork managerWithBaseURL:baseUrl sessionConfiguration:NO];
	[manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
		
	} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		
		
		id dic = [GMAFNetWork responseConfiguration:responseObject];
		
		success(task,dic);
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		fail(task,error);
	}];
	
}





#pragma mark POST
/**
 *  普通post方法请求网络数据
 *
 *  @param url     请求网址路径
 *  @param params  请求参数
 *  @param success 成功回调
 *  @param fail    失败回调
 */
+(void)POST:(NSString *)url params:(NSDictionary *)params
	success:(GMResponseSuccess)success fail:(GMResponseFail)fail{
	
	AFHTTPSessionManager *manager = [GMAFNetWork managerWithBaseURL:nil sessionConfiguration:NO];
	
	[manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		
		id dic = [GMAFNetWork responseConfiguration:responseObject];
		
		success(task,dic);
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		fail(task,error);
	}];
}



/**
 *  含有baseURL的post方法
 *
 *  @param url     请求网址路径
 *  @param baseUrl 请求网址根路径
 *  @param params  请求参数
 *  @param success 成功回调
 *  @param fail    失败回调
 */
+(void)POST:(NSString *)url baseURL:(NSString *)baseUrl params:(NSDictionary *)params
	success:(GMResponseSuccess)success fail:(GMResponseFail)fail{
	
	AFHTTPSessionManager *manager = [GMAFNetWork managerWithBaseURL:baseUrl sessionConfiguration:NO];
	[manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
		
	} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		
		id dic = [GMAFNetWork responseConfiguration:responseObject];
		
		success(task,dic);
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		fail(task,error);
	}];
}




#pragma mark UPLOAD
/**
 *  普通路径上传文件
 *
 *  @param url      请求网址路径
 *  @param params   请求参数
 *  @param filedata 文件
 *  @param name     指定参数名
 *  @param filename 文件名（要有后缀名）
 *  @param mimeType 文件类型
 *  @param progress 上传进度
 *  @param success  成功回调
 *  @param fail     失败回调
 */
+(void)uploadWithURL:(NSString *)url params:(NSDictionary *)params fileData:(NSData *)filedata name:(NSString *)name fileName:(NSString *)filename mimeType:(NSString *) mimeType progress:(GMProgress)progress success:(GMResponseSuccess)success fail:(GMResponseFail)fail{
	
	AFHTTPSessionManager *manager = [GMAFNetWork managerWithBaseURL:nil sessionConfiguration:NO];
	
	[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
		
		[formData appendPartWithFileData:filedata name:name fileName:filename mimeType:mimeType];
		
	} progress:^(NSProgress * _Nonnull uploadProgress) {
		
		progress(uploadProgress);
		
	} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		
		id dic = [GMAFNetWork responseConfiguration:responseObject];
		success(task,dic);
		
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		fail(task,error);
	}];
}



/**
 *  含有跟路径的上传文件
 *
 *  @param url      请求网址路径
 *  @param baseurl  请求网址根路径
 *  @param params   请求参数
 *  @param filedata 文件
 *  @param name     指定参数名
 *  @param filename 文件名（要有后缀名）
 *  @param mimeType 文件类型
 *  @param progress 上传进度
 *  @param success  成功回调
 *  @param fail     失败回调
 */
+(void)uploadWithURL:(NSString *)url
			 baseURL:(NSString *)baseurl
			  params:(NSDictionary *)params
			fileData:(NSData *)filedata
				name:(NSString *)name
			fileName:(NSString *)filename
			mimeType:(NSString *) mimeType
			progress:(GMProgress)progress
			 success:(GMResponseSuccess)success
				fail:(GMResponseFail)fail{
	
	AFHTTPSessionManager *manager = [GMAFNetWork managerWithBaseURL:baseurl sessionConfiguration:YES];
	
	[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
		[formData appendPartWithFileData:filedata name:name fileName:filename mimeType:mimeType];
	} progress:^(NSProgress * _Nonnull uploadProgress) {
		progress(uploadProgress);
	} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
		
		id dic = [GMAFNetWork responseConfiguration:responseObject];
		
		success(task,dic);
		
		
		success(task,responseObject);
	} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
		fail(task,error);
	}];
}




#pragma mark DOWN
/**
 *  下载文件
 *
 *  @param url      请求网络路径
 *  @param fileURL  保存文件url
 *  @param progress 下载进度
 *  @param success  成功回调
 *  @param fail     失败回调
 *
 *  @return 返回NSURLSessionDownloadTask实例，可用于暂停继续，暂停调用suspend方法，重新开启下载调用resume方法
 */
+(NSURLSessionDownloadTask *)downloadWithURL:(NSString *)url
								 savePathURL:(NSURL *)fileURL
									progress:(GMProgress )progress
									 success:(void (^)(NSURLResponse *, NSURL *))success
										fail:(void (^)(NSError *))fail{
	AFHTTPSessionManager *manager = [self managerWithBaseURL:nil sessionConfiguration:YES];
	
	NSURL *urlpath = [NSURL URLWithString:url];
	NSURLRequest *request = [NSURLRequest requestWithURL:urlpath];
	
	NSURLSessionDownloadTask *downloadtask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
		progress(downloadProgress);
		
	} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
		
		return [fileURL URLByAppendingPathComponent:[response suggestedFilename]];
	} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
		
		if (error) {
			fail(error);
		}else{
			
			success(response,filePath);
		}
	}];
	
	[downloadtask resume];
	
	return downloadtask;
}



#pragma mark - Private

+(AFHTTPSessionManager *)managerWithBaseURL:(NSString *)baseURL  sessionConfiguration:(BOOL)isconfiguration{
	
	NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
	AFHTTPSessionManager *manager =nil;
	
	NSURL *url = [NSURL URLWithString:baseURL];
	
	if (isconfiguration) {
		
		manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url sessionConfiguration:configuration];
	}else{
		manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
	}
	
	manager.requestSerializer = [AFHTTPRequestSerializer serializer];
	manager.responseSerializer = [AFHTTPResponseSerializer serializer];
	
	return manager;
}

+(id)responseConfiguration:(id)responseObject{
	
	NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
	string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
	NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
	NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
	return dic;
}
@end
